package com.example.back.Repository;

import com.example.back.Entity.MessageItem;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Repository
public interface MessageRepository extends JpaRepository<MessageItem, Long> {
    @Transactional
    @Modifying
    @Query("update MessageItem m set m.messageRead = true where m.messageReceiverId=:receiverId and m.messageSenderId =:senderId")
    void setMessageReadByMessageSenderIdAndMessReceiverId(@Param("senderId") long senderId,
                                                          @Param("receiverId") long receiverId);

    List<MessageItem> findAllByMessageReceiverIdAndMessageSenderId(long messageReceiverId, long messageSenderId);
    List<MessageItem> findAllByMessageReadAndMessageReceiverIdAndMessageSenderId(Sort sort, boolean isRead, long messageReceiverId, long messageSenderId);
    List<MessageItem> findAllByMessageReadAndMessageReceiverIdAndMessageSenderId(boolean isRead, long messageReceiverId, long messageSenderId);
}
